package edu.cuit.zhuyimeng.uaa.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import edu.cuit.zhuyimeng.framework.common.result.CommonResult;
import edu.cuit.zhuyimeng.uaa.controller.vo.UserInfoVO;
import edu.cuit.zhuyimeng.uaa.service.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Tag(name = "鉴权接口")
@RequestMapping("/auth")
@RequiredArgsConstructor
public class AuthenticationController {

    private final IUserService userService;

    /**
     * 获取用户权限列表
     */
    @Operation(summary = "获取用户权限列表",security = @SecurityRequirement(name = "Authorization"))
    @GetMapping("/permissions")
    @SaCheckLogin
    public CommonResult<List<String>> getPermissions() {
        return CommonResult.success(StpUtil.getPermissionList());
    }

    /**
     * 获取用户角色列表
     */
    @Operation(summary = "获取用户角色列表",security = @SecurityRequirement(name = "Authorization"))
    @GetMapping("/roles")
    @SaCheckLogin
    public CommonResult<List<String>> getRoles() {
        return CommonResult.success(StpUtil.getRoleList());
    }

    /**
     * 获取用户主要角色
     */
    @Operation(summary = "获取用户主要角色",security = @SecurityRequirement(name = "Authorization"))
    @GetMapping("/origin-roles")
    @SaCheckLogin
    public CommonResult<String> getOriginRoles() {
        return CommonResult.success(StpUtil.getRoleList().get(0));
    }

    /**
     * 查看用户信息
     *
     * @return 用户信息
     */
    @Operation(summary = "查看用户信息", security = @SecurityRequirement(name = "Authorization"))
    @GetMapping("/user")
    @SaCheckLogin
    public CommonResult<UserInfoVO> getUserInfo() {
        return CommonResult.success(userService.getUserInfoByLoginId(StpUtil.getLoginId().toString()));
    }

}
